home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Window;
-
- public class CheckersGame extends Applet {
- final int CHECKER_NONE;
- final int CHECKER_RED = 1;
- final int CHECKER_BLACK = 2;
- final int CHECKER_RED_KING = 3;
- final int CHECKER_BLACK_KING = 4;
- final int SQUARE_RED;
- final int SQUARE_BLACK = 1;
- final int squaresPerSide = 8;
- final int pixelsPerBoard = 400;
- final int pixelsPerSquare = 50;
- final int MOVE_INVALID;
- final int MOVE_VALID = 1;
- final int MOVE_HANDLED = 2;
- int[][] pieces = new int[8][8];
- int[][] board = new int[8][8];
- Image blackChecker;
- Image redChecker;
- Image blackKingChecker;
- Image redKingChecker;
- int clipX;
- int clipY;
- int clipWidth;
- int clipHeight;
- int moving_piece;
- int moving_piece_xpos;
- int moving_piece_ypos;
- int moving_piece_x;
- int moving_piece_y;
-
- private int sign(int value) {
- if (value > 0) {
- value = 1;
- } else if (value < 0) {
- value = -1;
- }
-
- return value;
- }
-
- private void SetUpdateRegion(int xpos, int ypos) {
- int halfPixelsPerSquare = 25;
- Dimension d = ((Component)this).getSize();
- int x1 = Math.max(0, Math.min(xpos, this.moving_piece_xpos) - halfPixelsPerSquare);
- int y1 = Math.max(0, Math.min(ypos, this.moving_piece_ypos) - halfPixelsPerSquare);
- int x2 = Math.min(d.width - 1, Math.max(xpos, this.moving_piece_xpos) + halfPixelsPerSquare);
- int y2 = Math.min(d.height - 1, Math.max(ypos, this.moving_piece_ypos) + halfPixelsPerSquare);
- this.clipX = x1;
- this.clipY = y1;
- this.clipWidth = x2 - x1;
- this.clipHeight = y2 - y1;
- }
-
- private int validMove(int piece, int oldX, int oldY, int newX, int newY) {
- int dx = newX - oldX;
- int dy = newY - oldY;
- if (newX >= 0 && newX < 8 && newY >= 0 && newY < 8) {
- if (this.board[newY][newX] == 0) {
- return 0;
- } else if (this.pieces[newY][newX] != 0) {
- return 0;
- } else {
- switch (piece) {
- case 0:
- default:
- return 0;
- case 1:
- if (dy < 1) {
- return 0;
- }
-
- if (Math.abs(dx) == 1 && dy == 1) {
- if (newY == 7) {
- this.pieces[newY][newX] = 3;
- return 2;
- }
-
- return 1;
- }
-
- if (Math.abs(dx) == 2 && dy == 2) {
- if (this.pieces[oldY + 1][oldX + this.sign(dx)] != 2 && this.pieces[oldY + 1][oldX + this.sign(dx)] != 4) {
- return 0;
- }
-
- this.pieces[oldY + 1][oldX + this.sign(dx)] = 0;
- if (newY == 7) {
- this.pieces[newY][newX] = 3;
- return 2;
- }
-
- return 1;
- }
- break;
- case 2:
- if (dy > -1) {
- return 0;
- }
-
- if (Math.abs(dx) == 1 && dy == -1) {
- if (newY == 0) {
- this.pieces[newY][newX] = 4;
- return 2;
- }
-
- return 1;
- }
-
- if (Math.abs(dx) == 2 && dy == -2) {
- if (this.pieces[oldY - 1][oldX + this.sign(dx)] != 1 && this.pieces[oldY - 1][oldX + this.sign(dx)] != 3) {
- return 0;
- }
-
- this.pieces[oldY - 1][oldX + this.sign(dx)] = 0;
- if (newY == 0) {
- this.pieces[newY][newX] = 4;
- return 2;
- }
-
- return 1;
- }
- break;
- case 3:
- if (Math.abs(dx) == 1 && Math.abs(dy) == 1) {
- return 1;
- }
-
- if (Math.abs(dx) == 2 && Math.abs(dy) == 2) {
- if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 2) {
- this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
- return 1;
- }
-
- if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 4) {
- this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
- return 1;
- }
-
- return 0;
- }
- break;
- case 4:
- if (Math.abs(dx) == 1 && Math.abs(dy) == 1) {
- return 1;
- }
-
- if (Math.abs(dx) == 2 && Math.abs(dy) == 2) {
- if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 1) {
- this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
- return 1;
- }
-
- if (this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] == 3) {
- this.pieces[oldY + this.sign(dy)][oldX + this.sign(dx)] = 0;
- return 1;
- }
-
- return 0;
- }
- }
-
- return 0;
- }
- } else {
- return 0;
- }
- }
-
- public void init() {
- int xcolor = 0;
- int ycolor = 0;
- this.blackChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "blackChecker.gif");
- this.redChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "redChecker.gif");
- this.blackKingChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "blackCheckerKing.gif");
- this.redKingChecker = ((Applet)this).getImage(((Applet)this).getCodeBase(), "redCheckerKing.gif");
-
- for(int x = 0; x < 8; ++x) {
- ycolor = xcolor % 2;
-
- for(int y = 0; y < 8; ++y) {
- this.board[y][x] = ycolor % 2 == 1 ? 1 : 0;
- ++ycolor;
- }
-
- ++xcolor;
- }
-
- for(int var6 = 0; var6 < 8; ++var6) {
- for(int y = 0; y < 8; ++y) {
- this.pieces[y][var6] = 0;
- }
- }
-
- for(int y = 0; y < 3; ++y) {
- for(int var7 = 1 - y % 2; var7 < 8; var7 += 2) {
- this.pieces[y][var7] = 1;
- }
- }
-
- for(int var11 = 5; var11 < 8; ++var11) {
- for(int var8 = 1 - var11 % 2; var8 < 8; var8 += 2) {
- this.pieces[var11][var8] = 2;
- }
- }
-
- ((Applet)this).resize(400, 400);
- }
-
- public void paint(Graphics g) {
- Color redColor = Color.red;
- Color blackColor = Color.black;
- Dimension d = ((Component)this).getSize();
-
- for(int x = 0; x < 8; ++x) {
- for(int y = 0; y < 8; ++y) {
- switch (this.board[y][x]) {
- case 0:
- g.setColor(redColor);
- break;
- default:
- g.setColor(blackColor);
- }
-
- int xpos = x * 50;
- int ypos = y * 50;
- g.fillRect(xpos, ypos, 50, 50);
- }
- }
-
- for(int var13 = 0; var13 < 8; ++var13) {
- for(int y = 0; y < 8; ++y) {
- int xpos = var13 * 50;
- int ypos = y * 50;
- switch (this.pieces[y][var13]) {
- case 0:
- default:
- break;
- case 1:
- g.drawImage(this.redChecker, xpos, ypos, this);
- break;
- case 2:
- g.drawImage(this.blackChecker, xpos, ypos, this);
- break;
- case 3:
- g.drawImage(this.redKingChecker, xpos, ypos, this);
- break;
- case 4:
- g.drawImage(this.blackKingChecker, xpos, ypos, this);
- }
- }
- }
-
- int xpos = this.moving_piece_xpos - 25;
- int ypos = this.moving_piece_ypos - 25;
- switch (this.moving_piece) {
- case 0:
- default:
- break;
- case 1:
- g.drawImage(this.redChecker, xpos, ypos, this);
- break;
- case 2:
- g.drawImage(this.blackChecker, xpos, ypos, this);
- break;
- case 3:
- g.drawImage(this.redKingChecker, xpos, ypos, this);
- break;
- case 4:
- g.drawImage(this.blackKingChecker, xpos, ypos, this);
- }
-
- this.clipX = 0;
- this.clipY = 0;
- this.clipWidth = d.width;
- this.clipHeight = d.height;
- }
-
- public void update(Graphics g) {
- g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
- this.paint(g);
- }
-
- public boolean mouseDown(Event evt, int xpos, int ypos) {
- int x = xpos / 50;
- int y = ypos / 50;
- if (x >= 0 && x < 8 && y >= 0 && y < 8) {
- if (this.pieces[y][x] != 0) {
- this.moving_piece = this.pieces[y][x];
- this.pieces[y][x] = 0;
- this.moving_piece_xpos = xpos;
- this.moving_piece_ypos = ypos;
- this.moving_piece_x = x;
- this.moving_piece_y = y;
- ((Component)this).repaint();
- } else {
- this.moving_piece = 0;
- }
- }
-
- return true;
- }
-
- public boolean mouseUp(Event evt, int xpos, int ypos) {
- int x = xpos / 50;
- int y = ypos / 50;
- if (this.moving_piece == 0) {
- return false;
- } else {
- int moveStatus = this.validMove(this.moving_piece, this.moving_piece_x, this.moving_piece_y, x, y);
- switch (moveStatus) {
- case 0:
- this.pieces[this.moving_piece_y][this.moving_piece_x] = this.moving_piece;
- break;
- case 1:
- this.pieces[y][x] = this.moving_piece;
- case 2:
- }
-
- this.moving_piece = 0;
- ((Component)this).repaint();
- return true;
- }
- }
-
- public void mouseExit() {
- if (this.moving_piece != 0) {
- this.pieces[this.moving_piece_y][this.moving_piece_x] = this.moving_piece;
- this.moving_piece = 0;
- ((Component)this).repaint();
- }
- }
-
- public boolean mouseDrag(Event evt, int xpos, int ypos) {
- if (this.moving_piece == 0) {
- return false;
- } else {
- this.SetUpdateRegion(xpos, ypos);
- this.moving_piece_xpos = xpos;
- this.moving_piece_ypos = ypos;
- ((Component)this).repaint();
- return true;
- }
- }
-
- public static void main(String[] args) {
- Frame f = new Frame("Checkers game");
- CheckersGame app = new CheckersGame();
- app.init();
- ((Applet)app).start();
- ((Container)f).add("Center", app);
- ((Component)f).setSize(300, 300);
- ((Window)f).show();
- }
-
- public CheckersGame() {
- this.clipWidth = ((Component)this).getSize().width;
- this.clipHeight = ((Component)this).getSize().height;
- this.moving_piece = 0;
- this.moving_piece_x = -1;
- this.moving_piece_y = -1;
- }
- }
-